package gui.options;

import gui.ECMUI;

import java.awt.GridLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ChannelOptionsLayout implements ComponentListener {

	private JPanel followedChannelsHolder = null; //this is the top-level pane that holds the graphical list of all followed channels
	private GridLayout layout = null;
	private int panelWidth = 0;
	
	public ChannelOptionsLayout(JPanel followedChannelsHolder) {
		this.followedChannelsHolder = followedChannelsHolder;
		
		FollowedChannelPanel fcp = new FollowedChannelPanel("");
		this.panelWidth = fcp.getWidth();
		
		int columns = this.panelWidth == 0 ? 3 : fcp.getWidth() / this.panelWidth;
		this.layout = new GridLayout(0, columns); //unlimited rows, columns are set the the width of the panel / width of the subcomponent
		followedChannelsHolder.setLayout(this.layout);
		
		ECMUI.addComponentListenerToFrame(this);
	}

	@Override
	public void componentResized(ComponentEvent e) {
		FollowedChannelPanel fcp = new FollowedChannelPanel("Test Channel Something"); //relatively long name, so that I can add long names to the other components and not worry about them disappearing because of the layout
		JFrame frame = new JFrame(); //I really don't like doing this...but I can't get the FollowedChannelPanel to show the correct width otherwise
		frame.add(fcp);
		frame.pack();
		int cols = 3; //default
		if (fcp.getWidth() != 0) {
			cols = (this.followedChannelsHolder.getWidth() / fcp.getWidth());
		}
		if (cols == 0) cols = 1; //just in case the previous if statements comes up as a 0, i don't want any errors
		this.layout.setColumns(cols); //adjust the number of columns based on how wide the window is
	}

	@Override
	public void componentMoved(ComponentEvent e) {
		//do nothing
	}

	@Override
	public void componentShown(ComponentEvent e) {
		//do nothing
	}

	@Override
	public void componentHidden(ComponentEvent e) {
		//do nothing
	}
}
